﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DTS
{
    class AlgorithmMACD : Algorithm
    {
        int PeriodShort;
        int PeriodLong;
        int PeriodSignal;
        public AlgorithmMACD(int ps,int pl, int psignal)
        {
            PeriodShort = ps;
            PeriodLong = pl;
            PeriodSignal = psignal;
            this.name = "MACD" + ps.ToString() + "-" + pl.ToString() + "-" + psignal.ToString();
        }
        public AlgorithmMACD()
        {
            PeriodShort = 12;
            PeriodLong = 26;
            PeriodSignal = 9;
            this.name = "MACD12-26-9";
        }
        public override int getDecision(HistoriaSpolki hs, DateTime Today)
        {
            List<Double> MACDline = new List<double>();
            int licznik = 0;
            while (MACDline.Count < 9)
            {
                if (!hs.czyDzienSesja(Today.AddDays(-licznik)))
                {
                    licznik++; continue;
                }
                MACDline.Add(MACD(hs, Today.AddDays(-licznik)));
                licznik++;
            }
            Double signal = Pomocnik.EMA(MACDline);
            if (MACDline[0] > signal  && MACDline[1] < signal ) return 1;
            if (MACDline[0] < signal  && MACDline[1] > signal ) return -1;
            return 0;
        }
        public override int getDecision(HistoriaSpolki hs, double cena, DateTime Today)
        {

            return this.getDecision(hs, Today);
        }

        private Double MACD(HistoriaSpolki hs, DateTime Today)
        {
            var dane = hs.getPrevOfData(Today, PeriodLong).getDane();
            List<Double> wartosc = new List<double>();
            foreach (var tmp in dane)
                wartosc.Add(tmp.GetClose());
            Double longEMA = Pomocnik.EMA(wartosc);
            dane = hs.getPrevOfData(Today, PeriodShort).getDane();
            wartosc.Clear();
            foreach (var tmp in dane)
                wartosc.Add(tmp.GetClose());
            Double shortEMA = Pomocnik.EMA(wartosc);
            return shortEMA - longEMA;
        }
    }
}
